/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package term_project;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Ross
 */
public class Chat extends User
{
    private ArrayList<User> members;                                            // Active member list
    private ArrayList<Message> messageList;                                     // Chat's messages
    
    public Chat(int id, String chatName)
    {
        super(id, chatName);                                                    // Calls the parent constructor
        messageList = new ArrayList();                                          // Creates new message list.
        members = new ArrayList();                                              // Creates a new member arraylist
        loadActiveUsers();
    }
    public ArrayList<User> getMembers()
    {
        return members;
    }
    public ArrayList<Message> getMessages()
    {
        return messageList;
    }
    public void sendMessage(User Sender, String Message)
    // PRE:  Requires the sender Id, the recipient ID and the desired message to send
    // POST: Adds a message to the database and returns true
    {
        Message tempMessage = new Message(Sender, ((User)this), Message);
        messageList.add(tempMessage);
    }
    public void loadMessages()
    // PRE:  
    // POST: Reads the messages from the database
    {
        // Runs every second
        // Parse the database for new messages
        // Add each message to the ArrayList for messages
        // Display each new message.
        Message tempMessage = new Message(new User(43, "Billy"), new User(50, "John Doe"), "Test message from \"server\"");
        messageList.add(tempMessage);
    }
    
    // ***************************
    // * Accessors               *
    // ***************************
    public User getUser(int index)
    {
        return members.get(index);
    }
    public void clearUsers()
    {
        members = new ArrayList();
    }
    // ****************************
    // * Loading functions        *
    // ****************************
    public void addUser(User user)
    {
        members.add(user);
    }
    private void loadActiveUsers()
    // Pre:  Must be in a valid chat room
    // Post: Returns the active users of the list
    {
                                           // Active users in the chat
        members = new ArrayList();                                          // Creats a new user arraylist
        
        // TODO: Load active users from the database
       
        // **************************
        // * Temporary user adding  *
        // **************************
        members.add(new User(0, "Admin"));
        members.add(new User(1, "John Smith"));

    }
    
    // **********************
    // * Static functins    *
    // **********************
    public static Chat newChat(String newChat)
    {
        int ID;                                                                 // creates the id variable
        Chat newC;                                                              // Creates the chat variable to return
        ID = Chat.addChatToDatabase();                                          // Creates a chat in the database
        newC = new Chat(ID, newChat);                                           // Instantiates the new chat
        return newC;                                                            // Returns the new chat
    }
    private static int addChatToDatabase()
    {
        Random ran = new Random();
        
        return ran.nextInt(5000);
    }
}